package no.rmz.chez.tournament;

/**
 * A rating value, used to rate players.
 */
public class Rating {

    /**
     * True iff the value represents a rating, false if
     * it represents a non-rating.
     *
     * @return true iff the object represents a valid rating.
     */
    public final boolean isRating() {
        return (ratingValue == 0);
    }

    /**
     * The state, a positive double.  Zero means that
     * the player is unrated.
     */
    private Double  ratingValue;

    /**
     * Create an unrated rating.
     *
     */
    public Rating() {
        this(0.0);
    }

    /**
     * String to use when raising illegal state exception.
     */
    private static final String ILLEGAL_STATE_GRIPE =
        "Ratings cannot have values less than zero";

    /**
     * Check if a new value is legal, raise an exception if
     * it isn't.
     * @param value potential new value
     */
    private static void  checkNewValue(final Double value) {
        if (value < 0) {
            throw new IllegalStateException(ILLEGAL_STATE_GRIPE);
        }
    }

    /**
     * Create a new rating.
     * @param value the initial value of this rating.
     */
    public Rating(final Double value) {
        super();
        checkNewValue(value);
        ratingValue = value;
    }

    /**
     * Get the rating value for this rating.
     * @return the rating value.
     */
    public final Double getRatingValue() {
        return ratingValue;
    }

    /**
     * Set a new rating value for this rating.
     * @param value the new rating value.
     */
    public final void setRatingValue(final Double value) {
        checkNewValue(value);
        this.ratingValue = value;
    }
}
